#pragma once

// CBrowserTab
enum USER_AGENT_TYPE;

class CSKEYBid;
class CWebkitView;
struct IWebURLRequest;
class CBrowserTab : public CTabCtrl
{
	DECLARE_DYNAMIC(CBrowserTab)

public:
	CBrowserTab();
	~CBrowserTab() override;
	CSKEYBid * m_pMain;

protected:
	DECLARE_MESSAGE_MAP()

protected:
	bool m_initialized, m_loading;
	USER_AGENT_TYPE m_uaType;
	TICK m_lastCheckTick;
	wchar_t m_resTag[40];

public:
	afx_msg void OnSize(UINT nType, int cx, int cy);

	void done();
	__forceinline bool initialized(){ return m_initialized; }
	__forceinline void setUserAgent(USER_AGENT_TYPE uaType){ m_uaType = uaType; }
	bool setResTag(LPCWSTR resTag);
	__forceinline LPCWSTR getResTag() { return m_resTag; }

	CWebkitView * getWebPage(int nItem);
	__forceinline CWebkitView * getWebPage(){ return getWebPage(GetCurSel()); }
	int getNItem(const CWebkitView * webPage);

	bool addWebPage(LPCWSTR url, bool selAfterAdded);
	bool addWebPage(IWebURLRequest* refRequest, bool selAfterAdded);
	bool closeWebPage(int nItem, bool closeLastPage=false);
	bool closeWebPage(CWebkitView * webPage){ return closeWebPage(getNItem(webPage)); }
	void OnSelchange();
	void checkLoading();
	bool changeTabTitle(int nItem, LPWSTR title);
	bool changeTabTitle(int nItem, HBITMAP hBitmap);
	bool changeTabTitle(int nItem, UINT idIcon);
	__forceinline bool changeTabTitle(int nItem, LPWSTR title, HBITMAP hBitmap)
	{ return changeTabTitle(nItem, title) && changeTabTitle(nItem, hBitmap); }
	__forceinline bool changeTabTitle(int nItem, LPWSTR title, UINT idIcon)
	{ return changeTabTitle(nItem, title) && changeTabTitle(nItem, idIcon);	}
	__forceinline bool changeTabTitle(CWebkitView * webPage, LPWSTR title, HBITMAP hBitmap)
	{ return changeTabTitle(getNItem(webPage), title, hBitmap); }
	__forceinline bool changeTabTitle(CWebkitView * webPage, LPWSTR title, UINT idIcon)
	{ return changeTabTitle(getNItem(webPage), title, idIcon); }
	__forceinline bool changeTabTitle(CWebkitView * webPage, LPWSTR title)
	{ return changeTabTitle(getNItem(webPage), title); }
	__forceinline bool changeTabTitle(CWebkitView * webPage, HBITMAP hBitmap)
	{ return changeTabTitle(getNItem(webPage), hBitmap); }
	__forceinline bool changeTabTitle(CWebkitView * webPage, UINT idIcon)
	{ return changeTabTitle(getNItem(webPage), idIcon); }
	void FlashLoaded(CWebkitView * webPage, bool loaded);
#ifdef _JAVASCRIPT_HOOK_
	intptr_t GetFreeSWFNum();
#endif
protected:
	CWebkitView *newWebPage();

#ifndef _WEBKIT_NEW
public:
	bool init(CSKEYBid * pMain, USER_AGENT_TYPE uaType, intptr_t flashVersion, LPCWSTR resTag = nullptr, LPCWSTR initURL = nullptr);
protected:
	intptr_t m_flashVersion;
#else
public:
	bool init(CSKEYBid * pMain, USER_AGENT_TYPE uaType, LPCWSTR resTag = nullptr, LPCWSTR initURL = nullptr);
#endif
};
